package com.hrt.web.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.GiftInfor;
import com.hrt.bean.GiftType;
import com.hrt.bean.GiftTypeDir;
import com.hrt.util.PageUtils;
import com.hrt.web.product.service.GiftInforService;
import com.hrt.web.product.service.GiftTypeDirWebService;
import com.hrt.web.product.service.GiftTypeWebService;

@Controller
@RequestMapping(value = "/HRTWEB")
public class GiftTypeControllorWeb extends BaseController<GiftInfor> {
	/**礼品基本信息*/
	@Autowired
	private GiftInforService giftInforService;
	
	/**礼品分类*/
	@Autowired
	private GiftTypeWebService giftTypeWebService;
	
	/**礼品分类明细*/
	@Autowired
	private GiftTypeDirWebService giftTypeDirWebService;

	private List<GiftTypeDir> giftTypeDirList;
	
	public List<GiftTypeDir> getGiftTypeDirList() {
		return giftTypeDirList;
	}

	public void setGiftTypeDirList(List<GiftTypeDir> giftTypeDirList) {
		this.giftTypeDirList = giftTypeDirList;
	}

	/**
	 * 分页
	 * */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryByPage2")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 12;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(giftInforService.totalCount());
		System.out.println("giftInforService.totalCount()==>"+giftInforService.totalCount());

		List<GiftInfor> resultList = giftInforService.queryByPage(getPageIndex(),
				getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	/**
	 * 查看
	 * 
	 */
	@SuppressWarnings("null")
	@RequestMapping(value = "/product_index")
	public ModelAndView productIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		setPageSize(12);
		list = giftInforService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(giftInforService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();
		
		List<GiftType> typeWebList = giftTypeWebService.findDelParents();
		if(typeWebList != null || typeWebList.size() > 0){
			for(GiftType type : typeWebList){
				List<GiftType> typeSonWebList = giftTypeWebService.findDelSonById(type.getId());
				type.setSonType(typeSonWebList);
			}
		}
		view.addObject("pageResult", page);
		view.addObject("parentsList", typeWebList);
		return view;

	}
	
	
	/**点击分类之后出现相对应的礼品信息*/
//	@RequestMapping(value = "/findGiftById")
//	@ResponseBody
//	public ModelAndView findGiftById(
//			@RequestParam(value = "id", required = true) String sonId,
//			
//			HttpServletRequest request, HttpServletResponse response){
//		System.out.println("sonId===>"+sonId);
//		if(sonId==null || sonId==""){
//		}
//		return null;
//	}
	@RequestMapping(value = "/findGiftById")
	@ResponseBody
	public Map<String, PageUtils<GiftInfor>> findGiftById(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "ids", required = true) String sonIds,
			HttpServletRequest request) {
		
		Map<String, PageUtils<GiftInfor>> resultMap = new HashMap<String, PageUtils<GiftInfor>>();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);
		setPageSize(pageSize);
		List<GiftInfor> resultList = new ArrayList<GiftInfor>();
		setTotalCount(giftInforService.totalCount());
		if (sonIds == null) {
			throw new RuntimeException("礼品分类id有误");
		} else {
			
			List<Long> sonIdsList = giftTypeDirWebService.findGiftBySonId(sonIds);
			
			String sonIdStr = "";
			for(Long sonId : sonIdsList ){
				sonIdStr = sonIdStr+sonId+",";
			}
			if(sonIdStr != ""){
				sonIdStr=sonIdStr.substring(0,sonIdStr.length()-1);
			}
			
			resultList = giftInforService.queryById(sonIdStr, pageIndex,
					pageSize);
			
		}
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		resultMap.put("typePage", page);
		return resultMap;
	}
}
